/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.simulation;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
/**
 *
 * @author 29159
 */
public class frame extends JFrame{
    static JTextField t1,t2,t3,t4,t5;
    frame(){
        //加入菜单条
        MenuList ml = new MenuList(); 
        //左侧面板 Spring继承JPanel
        Spring s =new Spring();
        s.setBounds(0, 0, 600, 800);        
        Mouse mouse = new Mouse();
        s.setBackground(new Color(252,157,154));
        s.addMouseListener(mouse);
        s.addMouseMotionListener(mouse);
        new Thread(s).start();
        //右侧面板
        JPanel p = new JPanel();
        p.setBounds(600, 0, 440, 800);
        p.setLayout(null);
        p.setBackground(new Color(131,175,155));
        p.setFont(new Font("宋体",Font.BOLD,50));
        //右侧面板组件
        JLabel l1,l2,l3,l4,l5;
        JButton bt;
        l1 = new JLabel("弹簧刚度");
        l2 = new JLabel("阻尼常数");
        l3 = new JLabel("悬挂物体质量");
        l4 = new JLabel("弹簧的静止长度");
        l5 = new JLabel("重力加速度");
        l1.setBounds(40,30,180,45);
        l2.setBounds(40,150,180,45);
        l3.setBounds(40,270,180,45);
        l4.setBounds(40,390,180,45);
        l5.setBounds(40,510,180,45);
        t1 = new JTextField("默认值为5");
        t2 = new JTextField("默认值为0.5");
        t3 = new JTextField("默认值为1");
        t4 = new JTextField("默认值为400");
        t5 = new JTextField("默认值为9.8");
        t1.setBounds(220,30,180,45);
        t2.setBounds(220,150,180,45);
        t3.setBounds(220,270,180,45);
        t4.setBounds(220,390,180,45);
        t5.setBounds(220,510,180,45);
        bt = new JButton("更新数据");
        bt.setBounds(140,660,160,60);
        //点击按钮后将Spring的参数重新设置
        bt.addActionListener((ActionEvent e) -> {
            String str1,str2,str3,str4,str5;
            str1=frame.t1.getText();
            str2=frame.t2.getText();
            str3=frame.t3.getText();
            str4=frame.t4.getText();
            str5=frame.t5.getText();
            if(str1.matches("-?[0-9]+.?[0-9]{0,}")&&str2.matches("-?[0-9]+.?[0-9]{0,}")&&str3.matches("-?[0-9]+.?[0-9]{0,}")&&str4.matches("-?[0-9]+.?[0-9]{0,}")&&str5.matches("-?[0-9]+.?[0-9]{0,}")){
                double k = Double.valueOf(t1.getText());
                double b = Double.valueOf(t2.getText());
                double m = Double.valueOf(t3.getText());
                double R = Double.valueOf(t4.getText());
                double g = Double.valueOf(t5.getText());
                Spring.set(m,R,k,g,b);
            }
            else{
                JFrame Alert = new JFrame("Alert");
                JLabel l = new JLabel("请保证全部数据框内均为实数");
                l.setSize(200, 30);
                Alert.add(l);
                Alert.setLayout(null);
                Alert.setBounds(600, 400, 200, 70);
                Alert.setVisible(true);
            }
        });
        //在右侧面板中加入组件
        p.add(l1);p.add(l2);p.add(l3);p.add(l4);p.add(l5);
        p.add(t1);p.add(t2);p.add(t3);p.add(t4);p.add(t5);
        p.add(bt);
        //调节弹簧段数
        JLabel la = new JLabel("弹簧段数");
        JButton plus = new JButton("+");
        JButton minus = new JButton("-");
        la.setBounds(195,585,60,40);
        plus.setBounds(100, 585, 60, 40);
        minus.setBounds(280,585,60,40);
        plus.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int m = Spring.getn();
                Spring.setn(m+1);
            }
        });
        minus.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int m = Spring.getn();
                if(m==1){
                    JFrame help = new JFrame("警告");
                    help.setSize(200, 100);
                    help.setLocation(600, 500);
                    help.setLayout(null);
                    help.setVisible(true);
                    String h = "<html><body style=\"text-align:center\">弹簧段数已达到最小值</body></html>";
                    JLabel h1 = new JLabel(h);
                    h1.setBounds(20, 0, 200, 100);
                    help.add(h1);
                }
                else{
                    Spring.setn(m-1);
                }
            }
        });
        p.add(la);
        p.add(plus);
        p.add(minus);
        
        //在JFrame中加入左右面板
        this.setMenuBar(ml);
        this.add(s);
        this.add(p);
    }
}
